Namespace Forms.Dialogs

    Public Class RepeatedAppointmentDialog
        Inherits PhoenixFormBase

#Region " Windows Form Designer generated code "

        Public Sub New()
            MyBase.New()

            'This call is required by the Windows Form Designer.
            InitializeComponent()

            'Add any initialization after the InitializeComponent() call
            currDate = Date.Today
        End Sub

        'Form overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub

        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer

        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
        Friend WithEvents GroupBox2 As System.Windows.Forms.GroupBox
        Friend WithEvents WeeklyRadioButton As System.Windows.Forms.RadioButton
        Friend WithEvents MonthlyRadioButton As System.Windows.Forms.RadioButton
        Friend WithEvents WeeklyPanel As System.Windows.Forms.Panel
        Friend WithEvents Label1 As System.Windows.Forms.Label
        Friend WithEvents MondayCheckEdit As DevExpress.XtraEditors.CheckEdit
        Friend WithEvents TuesdayCheckEdit As DevExpress.XtraEditors.CheckEdit
        Friend WithEvents ThursdayCheckEdit As DevExpress.XtraEditors.CheckEdit
        Friend WithEvents WednesdayCheckEdit As DevExpress.XtraEditors.CheckEdit
        Friend WithEvents SaturdayCheckEdit As DevExpress.XtraEditors.CheckEdit
        Friend WithEvents FridayCheckEdit As DevExpress.XtraEditors.CheckEdit
        Friend WithEvents SundayCheckEdit As DevExpress.XtraEditors.CheckEdit
        Friend WithEvents EveryTextEdit As DevExpress.XtraEditors.TextEdit
        Friend WithEvents MonthlyPanel As System.Windows.Forms.Panel
        Friend WithEvents Label2 As System.Windows.Forms.Label
        Friend WithEvents EveryMonthTextEdit As DevExpress.XtraEditors.TextEdit
        Friend WithEvents DayTextEdit As DevExpress.XtraEditors.TextEdit
        Friend WithEvents EndByRadioButton As System.Windows.Forms.RadioButton
        Friend WithEvents EndAfterRadioButton As System.Windows.Forms.RadioButton
        Friend WithEvents OccurTextEdit As DevExpress.XtraEditors.TextEdit
        Friend WithEvents EndByDateEdit As DevExpress.XtraEditors.DateEdit
        Friend WithEvents CancelSimpleButton As DevExpress.XtraEditors.SimpleButton
        Friend WithEvents OKSimpleButton As DevExpress.XtraEditors.SimpleButton
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(RepeatedAppointmentDialog))
            Me.GroupBox1 = New System.Windows.Forms.GroupBox
            Me.WeeklyPanel = New System.Windows.Forms.Panel
            Me.EveryTextEdit = New DevExpress.XtraEditors.TextEdit
            Me.SundayCheckEdit = New DevExpress.XtraEditors.CheckEdit
            Me.SaturdayCheckEdit = New DevExpress.XtraEditors.CheckEdit
            Me.FridayCheckEdit = New DevExpress.XtraEditors.CheckEdit
            Me.ThursdayCheckEdit = New DevExpress.XtraEditors.CheckEdit
            Me.WednesdayCheckEdit = New DevExpress.XtraEditors.CheckEdit
            Me.TuesdayCheckEdit = New DevExpress.XtraEditors.CheckEdit
            Me.MondayCheckEdit = New DevExpress.XtraEditors.CheckEdit
            Me.Label1 = New System.Windows.Forms.Label
            Me.MonthlyPanel = New System.Windows.Forms.Panel
            Me.DayTextEdit = New DevExpress.XtraEditors.TextEdit
            Me.EveryMonthTextEdit = New DevExpress.XtraEditors.TextEdit
            Me.Label2 = New System.Windows.Forms.Label
            Me.MonthlyRadioButton = New System.Windows.Forms.RadioButton
            Me.WeeklyRadioButton = New System.Windows.Forms.RadioButton
            Me.GroupBox2 = New System.Windows.Forms.GroupBox
            Me.EndByDateEdit = New DevExpress.XtraEditors.DateEdit
            Me.OccurTextEdit = New DevExpress.XtraEditors.TextEdit
            Me.EndByRadioButton = New System.Windows.Forms.RadioButton
            Me.EndAfterRadioButton = New System.Windows.Forms.RadioButton
            Me.CancelSimpleButton = New DevExpress.XtraEditors.SimpleButton
            Me.OKSimpleButton = New DevExpress.XtraEditors.SimpleButton
            Me.GroupBox1.SuspendLayout()
            Me.WeeklyPanel.SuspendLayout()
            CType(Me.EveryTextEdit.Properties, System.ComponentModel.ISupportInitialize).BeginInit()
            CType(Me.SundayCheckEdit.Properties, System.ComponentModel.ISupportInitialize).BeginInit()
            CType(Me.SaturdayCheckEdit.Properties, System.ComponentModel.ISupportInitialize).BeginInit()
            CType(Me.FridayCheckEdit.Properties, System.ComponentModel.ISupportInitialize).BeginInit()
            CType(Me.ThursdayCheckEdit.Properties, System.ComponentModel.ISupportInitialize).BeginInit()
            CType(Me.WednesdayCheckEdit.Properties, System.ComponentModel.ISupportInitialize).BeginInit()
            CType(Me.TuesdayCheckEdit.Properties, System.ComponentModel.ISupportInitialize).BeginInit()
            CType(Me.MondayCheckEdit.Properties, System.ComponentModel.ISupportInitialize).BeginInit()
            Me.MonthlyPanel.SuspendLayout()
            CType(Me.DayTextEdit.Properties, System.ComponentModel.ISupportInitialize).BeginInit()
            CType(Me.EveryMonthTextEdit.Properties, System.ComponentModel.ISupportInitialize).BeginInit()
            Me.GroupBox2.SuspendLayout()
            CType(Me.EndByDateEdit.Properties, System.ComponentModel.ISupportInitialize).BeginInit()
            CType(Me.OccurTextEdit.Properties, System.ComponentModel.ISupportInitialize).BeginInit()
            Me.SuspendLayout()
            '
            'GroupBox1
            '
            Me.GroupBox1.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                        Or System.Windows.Forms.AnchorStyles.Left) _
                        Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
            Me.GroupBox1.Controls.Add(Me.WeeklyPanel)
            Me.GroupBox1.Controls.Add(Me.MonthlyRadioButton)
            Me.GroupBox1.Controls.Add(Me.WeeklyRadioButton)
            Me.GroupBox1.Location = New System.Drawing.Point(8, 8)
            Me.GroupBox1.Name = "GroupBox1"
            Me.GroupBox1.Size = New System.Drawing.Size(400, 136)
            Me.GroupBox1.TabIndex = 0
            Me.GroupBox1.TabStop = False
            Me.GroupBox1.Text = "Recurrence pattern"
            '
            'WeeklyPanel
            '
            Me.WeeklyPanel.Controls.Add(Me.EveryTextEdit)
            Me.WeeklyPanel.Controls.Add(Me.SundayCheckEdit)
            Me.WeeklyPanel.Controls.Add(Me.SaturdayCheckEdit)
            Me.WeeklyPanel.Controls.Add(Me.FridayCheckEdit)
            Me.WeeklyPanel.Controls.Add(Me.ThursdayCheckEdit)
            Me.WeeklyPanel.Controls.Add(Me.WednesdayCheckEdit)
            Me.WeeklyPanel.Controls.Add(Me.TuesdayCheckEdit)
            Me.WeeklyPanel.Controls.Add(Me.MondayCheckEdit)
            Me.WeeklyPanel.Controls.Add(Me.Label1)
            Me.WeeklyPanel.Controls.Add(Me.MonthlyPanel)
            Me.WeeklyPanel.Location = New System.Drawing.Point(80, 16)
            Me.WeeklyPanel.Name = "WeeklyPanel"
            Me.WeeklyPanel.Size = New System.Drawing.Size(304, 80)
            Me.WeeklyPanel.TabIndex = 2
            '
            'EveryTextEdit
            '
            Me.EveryTextEdit.EditValue = "1"
            Me.EveryTextEdit.Location = New System.Drawing.Point(75, 5)
            Me.EveryTextEdit.Name = "EveryTextEdit"
            Me.EveryTextEdit.Size = New System.Drawing.Size(32, 20)
            Me.EveryTextEdit.TabIndex = 5
            '
            'SundayCheckEdit
            '
            Me.SundayCheckEdit.Location = New System.Drawing.Point(240, 32)
            Me.SundayCheckEdit.Name = "SundayCheckEdit"
            '
            'SundayCheckEdit.Properties
            '
            Me.SundayCheckEdit.Properties.Caption = "sunday"
            Me.SundayCheckEdit.Size = New System.Drawing.Size(72, 19)
            Me.SundayCheckEdit.TabIndex = 12
            '
            'SaturdayCheckEdit
            '
            Me.SaturdayCheckEdit.Location = New System.Drawing.Point(168, 56)
            Me.SaturdayCheckEdit.Name = "SaturdayCheckEdit"
            '
            'SaturdayCheckEdit.Properties
            '
            Me.SaturdayCheckEdit.Properties.Caption = "saturday"
            Me.SaturdayCheckEdit.Size = New System.Drawing.Size(75, 19)
            Me.SaturdayCheckEdit.TabIndex = 11
            '
            'FridayCheckEdit
            '
            Me.FridayCheckEdit.Location = New System.Drawing.Point(168, 32)
            Me.FridayCheckEdit.Name = "FridayCheckEdit"
            '
            'FridayCheckEdit.Properties
            '
            Me.FridayCheckEdit.Properties.Caption = "friday"
            Me.FridayCheckEdit.Size = New System.Drawing.Size(88, 19)
            Me.FridayCheckEdit.TabIndex = 10
            '
            'ThursdayCheckEdit
            '
            Me.ThursdayCheckEdit.Location = New System.Drawing.Point(80, 56)
            Me.ThursdayCheckEdit.Name = "ThursdayCheckEdit"
            '
            'ThursdayCheckEdit.Properties
            '
            Me.ThursdayCheckEdit.Properties.Caption = "thursday"
            Me.ThursdayCheckEdit.Size = New System.Drawing.Size(75, 19)
            Me.ThursdayCheckEdit.TabIndex = 9
            '
            'WednesdayCheckEdit
            '
            Me.WednesdayCheckEdit.Location = New System.Drawing.Point(80, 32)
            Me.WednesdayCheckEdit.Name = "WednesdayCheckEdit"
            '
            'WednesdayCheckEdit.Properties
            '
            Me.WednesdayCheckEdit.Properties.Caption = "wednesday"
            Me.WednesdayCheckEdit.Size = New System.Drawing.Size(88, 19)
            Me.WednesdayCheckEdit.TabIndex = 8
            '
            'TuesdayCheckEdit
            '
            Me.TuesdayCheckEdit.Location = New System.Drawing.Point(8, 56)
            Me.TuesdayCheckEdit.Name = "TuesdayCheckEdit"
            '
            'TuesdayCheckEdit.Properties
            '
            Me.TuesdayCheckEdit.Properties.Caption = "tuesday"
            Me.TuesdayCheckEdit.Size = New System.Drawing.Size(64, 19)
            Me.TuesdayCheckEdit.TabIndex = 7
            '
            'MondayCheckEdit
            '
            Me.MondayCheckEdit.Location = New System.Drawing.Point(8, 32)
            Me.MondayCheckEdit.Name = "MondayCheckEdit"
            '
            'MondayCheckEdit.Properties
            '
            Me.MondayCheckEdit.Properties.Caption = "monday"
            Me.MondayCheckEdit.Size = New System.Drawing.Size(64, 19)
            Me.MondayCheckEdit.TabIndex = 6
            '
            'Label1
            '
            Me.Label1.Location = New System.Drawing.Point(8, 8)
            Me.Label1.Name = "Label1"
            Me.Label1.Size = New System.Drawing.Size(176, 23)
            Me.Label1.TabIndex = 0
            Me.Label1.Text = "Recur every               week(s) on:"
            '
            'MonthlyPanel
            '
            Me.MonthlyPanel.Controls.Add(Me.DayTextEdit)
            Me.MonthlyPanel.Controls.Add(Me.EveryMonthTextEdit)
            Me.MonthlyPanel.Controls.Add(Me.Label2)
            Me.MonthlyPanel.Location = New System.Drawing.Point(0, 0)
            Me.MonthlyPanel.Name = "MonthlyPanel"
            Me.MonthlyPanel.Size = New System.Drawing.Size(304, 80)
            Me.MonthlyPanel.TabIndex = 3
            Me.MonthlyPanel.Visible = False
            '
            'DayTextEdit
            '
            Me.DayTextEdit.EditValue = ""
            Me.DayTextEdit.Location = New System.Drawing.Point(35, 12)
            Me.DayTextEdit.Name = "DayTextEdit"
            Me.DayTextEdit.Size = New System.Drawing.Size(32, 20)
            Me.DayTextEdit.TabIndex = 2
            '
            'EveryMonthTextEdit
            '
            Me.EveryMonthTextEdit.EditValue = "1"
            Me.EveryMonthTextEdit.Location = New System.Drawing.Point(125, 12)
            Me.EveryMonthTextEdit.Name = "EveryMonthTextEdit"
            Me.EveryMonthTextEdit.Size = New System.Drawing.Size(32, 20)
            Me.EveryMonthTextEdit.TabIndex = 3
            '
            'Label2
            '
            Me.Label2.Location = New System.Drawing.Point(8, 16)
            Me.Label2.Name = "Label2"
            Me.Label2.Size = New System.Drawing.Size(240, 23)
            Me.Label2.TabIndex = 0
            Me.Label2.Text = "Day               of every                 month(s)"
            '
            'MonthlyRadioButton
            '
            Me.MonthlyRadioButton.Location = New System.Drawing.Point(8, 48)
            Me.MonthlyRadioButton.Name = "MonthlyRadioButton"
            Me.MonthlyRadioButton.Size = New System.Drawing.Size(80, 24)
            Me.MonthlyRadioButton.TabIndex = 1
            Me.MonthlyRadioButton.Text = "Monthly"
            '
            'WeeklyRadioButton
            '
            Me.WeeklyRadioButton.Checked = True
            Me.WeeklyRadioButton.Location = New System.Drawing.Point(8, 16)
            Me.WeeklyRadioButton.Name = "WeeklyRadioButton"
            Me.WeeklyRadioButton.Size = New System.Drawing.Size(72, 24)
            Me.WeeklyRadioButton.TabIndex = 0
            Me.WeeklyRadioButton.TabStop = True
            Me.WeeklyRadioButton.Text = "Weekly"
            '
            'GroupBox2
            '
            Me.GroupBox2.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                        Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
            Me.GroupBox2.Controls.Add(Me.EndByDateEdit)
            Me.GroupBox2.Controls.Add(Me.OccurTextEdit)
            Me.GroupBox2.Controls.Add(Me.EndByRadioButton)
            Me.GroupBox2.Controls.Add(Me.EndAfterRadioButton)
            Me.GroupBox2.Location = New System.Drawing.Point(8, 120)
            Me.GroupBox2.Name = "GroupBox2"
            Me.GroupBox2.Size = New System.Drawing.Size(400, 80)
            Me.GroupBox2.TabIndex = 1
            Me.GroupBox2.TabStop = False
            Me.GroupBox2.Text = "Range of recurrence"
            '
            'EndByDateEdit
            '
            Me.EndByDateEdit.EditValue = New Date(2005, 4, 10, 0, 0, 0, 0)
            Me.EndByDateEdit.Location = New System.Drawing.Point(80, 48)
            Me.EndByDateEdit.Name = "EndByDateEdit"
            '
            'EndByDateEdit.Properties
            '
            Me.EndByDateEdit.Properties.Buttons.AddRange(New DevExpress.XtraEditors.Controls.EditorButton() {New DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Combo)})
            Me.EndByDateEdit.Size = New System.Drawing.Size(128, 20)
            Me.EndByDateEdit.TabIndex = 23
            '
            'OccurTextEdit
            '
            Me.OccurTextEdit.EditValue = "10"
            Me.OccurTextEdit.Location = New System.Drawing.Point(80, 16)
            Me.OccurTextEdit.Name = "OccurTextEdit"
            Me.OccurTextEdit.Size = New System.Drawing.Size(48, 20)
            Me.OccurTextEdit.TabIndex = 21
            '
            'EndByRadioButton
            '
            Me.EndByRadioButton.Location = New System.Drawing.Point(8, 48)
            Me.EndByRadioButton.Name = "EndByRadioButton"
            Me.EndByRadioButton.Size = New System.Drawing.Size(80, 24)
            Me.EndByRadioButton.TabIndex = 22
            Me.EndByRadioButton.Text = "End by:"
            '
            'EndAfterRadioButton
            '
            Me.EndAfterRadioButton.Checked = True
            Me.EndAfterRadioButton.Location = New System.Drawing.Point(8, 16)
            Me.EndAfterRadioButton.Name = "EndAfterRadioButton"
            Me.EndAfterRadioButton.Size = New System.Drawing.Size(240, 24)
            Me.EndAfterRadioButton.TabIndex = 20
            Me.EndAfterRadioButton.TabStop = True
            Me.EndAfterRadioButton.Text = "End after:                       occurrences"
            '
            'CancelSimpleButton
            '
            Me.CancelSimpleButton.Location = New System.Drawing.Point(336, 208)
            Me.CancelSimpleButton.Name = "CancelSimpleButton"
            Me.CancelSimpleButton.TabIndex = 25
            Me.CancelSimpleButton.Text = "&Cancel"
            '
            'OKSimpleButton
            '
            Me.OKSimpleButton.Location = New System.Drawing.Point(256, 208)
            Me.OKSimpleButton.Name = "OKSimpleButton"
            Me.OKSimpleButton.TabIndex = 24
            Me.OKSimpleButton.Text = "&OK"
            '
            'RepeatedAppointmentDialog
            '
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.ClientSize = New System.Drawing.Size(416, 238)
            Me.Controls.Add(Me.OKSimpleButton)
            Me.Controls.Add(Me.CancelSimpleButton)
            Me.Controls.Add(Me.GroupBox2)
            Me.Controls.Add(Me.GroupBox1)
            Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
            Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
            Me.MaximizeBox = False
            Me.MinimizeBox = False
            Me.Name = "RepeatedAppointmentDialog"
            Me.ShowInTaskbar = False
            Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent
            Me.Text = "Appointment Recurrence"
            Me.GroupBox1.ResumeLayout(False)
            Me.WeeklyPanel.ResumeLayout(False)
            CType(Me.EveryTextEdit.Properties, System.ComponentModel.ISupportInitialize).EndInit()
            CType(Me.SundayCheckEdit.Properties, System.ComponentModel.ISupportInitialize).EndInit()
            CType(Me.SaturdayCheckEdit.Properties, System.ComponentModel.ISupportInitialize).EndInit()
            CType(Me.FridayCheckEdit.Properties, System.ComponentModel.ISupportInitialize).EndInit()
            CType(Me.ThursdayCheckEdit.Properties, System.ComponentModel.ISupportInitialize).EndInit()
            CType(Me.WednesdayCheckEdit.Properties, System.ComponentModel.ISupportInitialize).EndInit()
            CType(Me.TuesdayCheckEdit.Properties, System.ComponentModel.ISupportInitialize).EndInit()
            CType(Me.MondayCheckEdit.Properties, System.ComponentModel.ISupportInitialize).EndInit()
            Me.MonthlyPanel.ResumeLayout(False)
            CType(Me.DayTextEdit.Properties, System.ComponentModel.ISupportInitialize).EndInit()
            CType(Me.EveryMonthTextEdit.Properties, System.ComponentModel.ISupportInitialize).EndInit()
            Me.GroupBox2.ResumeLayout(False)
            CType(Me.EndByDateEdit.Properties, System.ComponentModel.ISupportInitialize).EndInit()
            CType(Me.OccurTextEdit.Properties, System.ComponentModel.ISupportInitialize).EndInit()
            Me.ResumeLayout(False)

        End Sub

#End Region

        Private currDate As New Date
        Private currApp As DataAccess.DataSetAppointments.Appointment
        Private days As New ArrayList

        Private Sub MonthlyRadioButton_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MonthlyRadioButton.CheckedChanged
            WeeklyRadioButton.Checked = Not MonthlyRadioButton.Checked
            WeeklyPanel.Visible = WeeklyRadioButton.Checked
            MonthlyPanel.Visible = MonthlyRadioButton.Checked
        End Sub

        Private Sub WeeklyRadioButton_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WeeklyRadioButton.CheckedChanged
            WeeklyRadioButton.Checked = Not MonthlyRadioButton.Checked
            WeeklyPanel.Visible = WeeklyRadioButton.Checked
            MonthlyPanel.Visible = MonthlyRadioButton.Checked
        End Sub

        Public Sub Init(ByVal d As Date, ByVal app As DataAccess.DataSetAppointments.Appointment)
            currDate = d
            currApp = app
            Select Case currDate.DayOfWeek
                Case DayOfWeek.Sunday
                    SundayCheckEdit.Checked = True
                Case DayOfWeek.Friday
                    FridayCheckEdit.Checked = True
                Case DayOfWeek.Monday
                    MondayCheckEdit.Checked = True
                Case DayOfWeek.Saturday
                    SaturdayCheckEdit.Checked = True
                Case DayOfWeek.Thursday
                    ThursdayCheckEdit.Checked = True
                Case DayOfWeek.Tuesday
                    TuesdayCheckEdit.Checked = True
                Case DayOfWeek.Wednesday
                    WednesdayCheckEdit.Checked = True
            End Select

            DayTextEdit.Text = currDate.Day
        End Sub

        Private Sub OKSimpleButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OKSimpleButton.Click
            GetSelectedDay()
            If EndAfterRadioButton.Checked Then
                EndAfterGeneration()
            Else
                EndByGeneration()
            End If
            Close()
        End Sub

        Private Sub ConficDetected(ByVal app As DataAccess.DataSetAppointments.Appointment)
            AppMessageBox(String.Format(Res.ConflictRepeatedAppointment, app.StartTime), MsgBoxStyle.OKOnly Or MsgBoxStyle.Critical)
        End Sub

        Private Sub EndAfterGeneration()
            Dim i As Integer = 0
            Dim count As Integer = Integer.Parse(OccurTextEdit.Text)
            For i = 0 To count - 1
                If WeeklyRadioButton.Checked Then
                    currDate = GetNextDay(currDate)
                Else
                    currDate = GetNextMonthDay(currDate)
                End If
                Dim app As DataAccess.DataSetAppointments.Appointment = Helpers.AppointmentHelper.NewAppointment()
                Helpers.AppointmentHelper.Copy(app, currApp)
                app.StartTime = currDate
                Dim m As Integer = app.ExpectedDuration Mod 60
                Dim h As Double = app.ExpectedDuration / 60 - m / 60
                app.FinishTime = app.StartTime.AddHours(h)
                app.FinishTime = app.FinishTime.AddMinutes(m)
                If Helpers.AppointmentHelper.IsValidTime(app.StartTime, app.FinishTime, app) Then
                    Helpers.AppointmentHelper.UpdateAppointment(app, Me)
                Else
                    ConficDetected(app)
                    Exit For
                End If
            Next
        End Sub

        Private Sub EndByGeneration()
            Dim i As Integer = 0
            Dim endDate As Date = EndByDateEdit.DateTime
            If WeeklyRadioButton.Checked Then
                currDate = GetNextDay(currDate)
            Else
                currDate = GetNextMonthDay(currDate)
            End If
            While currDate.CompareTo(endDate) < 0
                Dim app As DataAccess.DataSetAppointments.Appointment = Helpers.AppointmentHelper.NewAppointment()
                Helpers.AppointmentHelper.Copy(app, currApp)
                app.StartTime = currDate
                Dim m As Integer = app.ExpectedDuration Mod 60
                Dim h As Double = app.ExpectedDuration / 60 - m / 60
                app.FinishTime = app.StartTime.AddHours(h)
                app.FinishTime = app.FinishTime.AddMinutes(m)
                If Helpers.AppointmentHelper.IsValidTime(app.StartTime, app.FinishTime, app) Then
                    Helpers.AppointmentHelper.UpdateAppointment(app, Me)
                Else
                    ConficDetected(app)
                    Exit While
                End If
                If WeeklyRadioButton.Checked Then
                    currDate = GetNextDay(currDate)
                Else
                    currDate = GetNextMonthDay(currDate)
                End If
            End While
        End Sub

        Private Sub GetSelectedDay()
            If SundayCheckEdit.Checked Then days.Add(DayOfWeek.Sunday)
            If MondayCheckEdit.Checked Then days.Add(DayOfWeek.Monday)
            If TuesdayCheckEdit.Checked Then days.Add(DayOfWeek.Tuesday)
            If WednesdayCheckEdit.Checked Then days.Add(DayOfWeek.Wednesday)
            If ThursdayCheckEdit.Checked Then days.Add(DayOfWeek.Thursday)
            If FridayCheckEdit.Checked Then days.Add(DayOfWeek.Friday)
            If SaturdayCheckEdit.Checked Then days.Add(DayOfWeek.Saturday)
        End Sub

        Private Function GetNextDay(ByVal d As Date) As Date
            Dim dat As New Date(d.Year, d.Month, d.Day, d.Hour, d.Minute, 0)
            While True
                dat = dat.AddDays(1)
                If days.Contains(dat.DayOfWeek) Then
                    Dim i As Integer
                    For i = 1 To Integer.Parse(EveryTextEdit.Text) - 1
                        dat.AddDays(7)
                    Next
                    Return dat
                End If
            End While
        End Function

        Private Function GetNextMonthDay(ByVal d As Date) As Date
            Dim nD As New Date(d.Year, d.Month, d.Day, d.Hour, d.Minute, 0)
            If nD.Day >= Integer.Parse(DayTextEdit.Text) Then
                nD = nD.AddMonths(Integer.Parse(EveryMonthTextEdit.Text))
            End If
            While True
                If nD.Day = Integer.Parse(DayTextEdit.Text) Then
                    Return nD
                End If
                nD.AddDays(-1)
            End While
        End Function

        Private Sub CancelSimpleButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CancelSimpleButton.Click
            Close()
        End Sub
    End Class

End Namespace
